package com.hanhanxiaochong.zhongxiangchuanmei.view.main.act;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.SimpleUserInfo;
import com.hanhanxiaochong.zhongxiangchuanmei.config.HttpPostService;
import com.hanhanxiaochong.zhongxiangchuanmei.util.MessageEvent;
import com.hanhanxiaochong.zhongxiangchuanmei.util.SharedPreferencesUtil;
import com.hanhanxiaochong.zhongxiangchuanmei.util.ToastUtils;
import com.hanhanxiaochong.zhongxiangchuanmei.util.glide.GlideImgManager;
import com.hanhanxiaochong.zhongxiangchuanmei.view.base.BaseLayout;
import com.hanhanxiaochong.zhongxiangchuanmei.view.my.act.AddressActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.my.act.MsgActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.my.act.MyCallUsActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.my.act.MyFavoriteActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.my.act.MyFeedbackActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.my.act.MyInviteActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.my.act.MyVipActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.my.act.SettingActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.my.act.ShoppingCartActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.my.act.UpdateUserInfoActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.widget.webview.WebActivity;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.BaseObserver;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.ResponseBase;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.RetrofitSource;

import butterknife.BindView;
import butterknife.OnClick;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;
import de.hdodenhof.circleimageview.CircleImageView;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Author : 贾桐
 * E-mail : 653773284@qq.com
 * Time   : 2018/4/5.
 * Desc   :
 */

public class MyLayout extends BaseLayout {

    @BindView(R.id.img_userhead)
    CircleImageView imgUserhead;
    @BindView(R.id.txt_username)
    TextView txtUsername;
    @BindView(R.id.txt_user_sign)
    TextView txtUserSign;

    public MyLayout(Context context) {
        super(context);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.layout_my;
    }

    @Override
    protected void initPresenter() {

    }

    @Override
    protected void init() {
        registerEventbus();
        updateUserInfo();
    }

    private void updateUserInfo() {
        RetrofitSource.createApi(HttpPostService.class).person_easy_info(SharedPreferencesUtil.getsInstances(mContext).getLoginInfo().getToken())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<SimpleUserInfo>>(this) {

                    @Override
                    public void onSuccess(ResponseBase<SimpleUserInfo> response) {
                        SimpleUserInfo data = response.getData();
                        GlideImgManager.glideLoader(mContext, data.getAvatar(), R.mipmap.img_error, R.mipmap.img_error, imgUserhead);
                        txtUsername.setText(data.getUser_nicename());
//                            txtUserSign.setText(data.getSignature());
                    }
                });
    }

    @OnClick({R.id.rl_set_userinfo, R.id.ll_my_vip, R.id.ll_my_wallet, R.id.ll_my_msg, R.id
            .ll_my_address, R.id.ll_my_favorites, R.id.ll_my_invite, R.id.ll_my_shopcar, R.id
            .ll_my_aboutus, R.id.ll_my_setting, R.id.ll_my_service, R.id.ll_my_alert, R.id
            .ll_my_feedback, R.id.top_view})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_set_userinfo:
                Intent userinfoIntent = new Intent(mContext, UpdateUserInfoActivity.class);
                mContext.startActivity(userinfoIntent);
                break;
            case R.id.ll_my_vip:
                Intent intent8 = new Intent(mContext, MyVipActivity.class);
                mContext.startActivity(intent8);
                break;
            case R.id.ll_my_wallet:
                Intent intent9 = new Intent(mContext, MyWalletActivity.class);
                mContext.startActivity(intent9);
                break;
            case R.id.ll_my_msg:
                Intent intent = new Intent(mContext, MsgActivity.class);
                mContext.startActivity(intent);
                break;
            case R.id.ll_my_address:
                mContext.startActivity(AddressActivity.createIntent(mContext, "my"));
                break;
            case R.id.ll_my_favorites:
                Intent intent2 = new Intent(mContext, MyFavoriteActivity.class);
                mContext.startActivity(intent2);
                break;
            case R.id.ll_my_invite:
                Intent intent3 = new Intent(mContext, MyInviteActivity.class);
                mContext.startActivity(intent3);
                break;
            case R.id.ll_my_shopcar:
                mContext.startActivity(ShoppingCartActivity.createIntent(mContext));
                break;
            case R.id.ll_my_aboutus:
//                Intent intent4 = new Intent(mContext, MyAboutUsActivity.class);
//                mContext.startActivity(intent4);
                mContext.startActivity(WebActivity.createIntent(mContext, "关于我们",
                        "https://mp.weixin.qq.com/s?__biz=MzUxNDcwNjIwMw==&mid=2247484409&idx=3&sn=29891f7c4b4a455b5c3d010eed7ca29c&chksm=f9409d8dce37149b0278ec72ba7ba02e3676c9639a0764935448ee6d9f14fb647f959347d900&token=773928473&lang=zh_CN#rd", null));
                break;
            case R.id.ll_my_setting:
                Intent settingIntent = new Intent(mContext, SettingActivity.class);
                mContext.startActivity(settingIntent);
                break;
            case R.id.ll_my_service:
                Intent intent6 = new Intent(mContext, MyCallUsActivity.class);
                mContext.startActivity(intent6);
                break;
            case R.id.ll_my_alert:
                if (SharedPreferencesUtil.getsInstances(mContext).getLoginInfo().getT_id() != null) {
                    Intent intent7 = new Intent(mContext, MyAlertActivity.class);
                    mContext.startActivity(intent7);
                } else {
                    ToastUtils.showToast("您不是技师");
                }
                break;
            case R.id.ll_my_feedback:
                Intent intent5 = new Intent(mContext, MyFeedbackActivity.class);
                mContext.startActivity(intent5);
                break;
            case R.id.top_view:
                Intent intent10 = new Intent(mContext, UpdateUserInfoActivity.class);
                mContext.startActivity(intent10);
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MainThread)
    public void handleEvent(MessageEvent event) {
        if (event.getMessage().equals("update_user_info")) {
            updateUserInfo();
        }
    }
}
